package com.atguigu.gulimail.order.listener;

import com.atguigu.common.vo.SeckillOrderTo;
import com.atguigu.gulimail.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author fangy
 * @date 2022-10-25 16:21
 */
@RabbitListener(queues = "order.seckill.order.queue")
@Component
public class OrderSeckillQueueListener {


    @Autowired
    OrderService orderService;


    @RabbitHandler
    public void listener(Message message, SeckillOrderTo seckillOrderTo, Channel channel) throws IOException {
        System.out.println("收到过期订单，准备关闭"+seckillOrderTo.getOrderSn());

        try {

            orderService.createSeckillOrder(seckillOrderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){

            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);

        }

    }


}
